#include "ncis.h"
#include "../log/log.h"
#include "../config.h"

int
main(void)
{
  msd_s myfirstsocket;
  CLNode_s * ncis_root = CL_create();
  npacket_s packet_to_send, packet_to_receive;
  char server_ip[] = "127.0.0.1";
  int ret;

  if( LOG_set_file(NULL) < 0 )
    return 1;
  LOG_set_level(LOG_ALL);


  char data_to_send[50];
  //sprintf(data_to_send,"list");
  sprintf(data_to_send,"neighbour 192.168.1.1:6789\n");
  //sprintf(data_to_send,"file \"poney.avi\" a1aaaa 10000 185.89.2.8:42");
  packet_to_send.datap = (void*) &data_to_send;
  packet_to_send.datalen = sizeof(data_to_send);

  char data_to_receive[MAX_NPACK_SIZE];
  packet_to_receive.datap = (void*) &data_to_receive;
  packet_to_receive.datalen = 0;
/*
  //serveur 
  OSOCK_connect( &myfirstsocket, TCP, server_ip , COMMAND_PORT, SERVER );
  OSOCK_send( &myfirstsocket, &packet_to_send );
  OSOCK_receive(&packet_to_receive, &myfirstsocket);
  ret = NCIS_np2ncis(ncis_root,&packet_to_receive);
  if(ret<0){
	printf("pb\n");
  }
  else{
  	NCIS_fprintf(stdout,(ncis_standard_s *)(CL_first(ncis_root)->data));
  }
*/
  //client

  OSOCK_connect( &myfirstsocket, TCP, server_ip , COMMAND_PORT, CLIENT ); 
  ret = OSOCK_send( &myfirstsocket, &packet_to_send );
  if(ret==0){
  	LOG_add(LOG_DEBUG,"Packet 1 envoye");
  }
  sprintf(data_to_send,"neighbourhood");

  packet_to_send.datalen=sizeof(data_to_send);
  sleep(2);
  OSOCK_send( &myfirstsocket, &packet_to_send );
  if(ret==0){
  	LOG_add(LOG_DEBUG,"Packet 1 envoye");
  }
  OSOCK_receive( &packet_to_receive, &myfirstsocket );
  ret = NCIS_np2ncis(ncis_root,&packet_to_receive);
  if(ret<0){
	printf("pb\n");
  }
  else{
  	NCIS_fprintf(stdout,(ncis_standard_s *)(CL_first(ncis_root)->data));
	//NCIS_ncis2np(&packet_to_send,ncis_root);
	//OSOCK_send( &myfirstsocket, &packet_to_send);
  }


  CL_map(ncis_root,free);
  CL_destroy(ncis_root);



  OSOCK_disconnect( &myfirstsocket );

  return EXIT_SUCCESS;

	/*
	
	
	ncis_standard_s ncis;
	npacket_s np;
	int ret;
	char data[450];
	sprintf(data,"file \"poney.avi.jpg\" 12345 23 127.2.8.3:65535");
	//OSOCK_fill_npacket(&np,data,sizeof(data));
	np.datap=(void *)data;
	np.datalen=450;
	ret = NCIS_np2ncis(&ncis,&np);
	if(ret<0){
		printf("pb\n");
	}
	else{
		NCIS_fprintf(stdout,&ncis);
	}
	
	return 0;

	*/

}
